package inits

import (
	"doctor-srv/basic/config"
	"log"

	"github.com/elastic/go-elasticsearch/v8"
)

var EsClient *elasticsearch.Client

// InitElasticsearch 初始化Elasticsearch连接
func InitElasticsearch() {
	cfg := config.Conf.Elasticsearch

	client, err := elasticsearch.NewClient(elasticsearch.Config{
		Addresses: []string{cfg.Address},
	})

	if err != nil {
		log.Fatalf("Failed to create Elasticsearch client: %v", err)
	}

	// 测试连接
	res, err := client.Info()
	if err != nil {
		log.Fatalf("Failed to connect to Elasticsearch: %v", err)
	}
	defer res.Body.Close()

	EsClient = client
	log.Println("Elasticsearch connected successfully")
}

